package com.baiduspring.utils.string;

import net.sf.json.JSONObject;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    public static String ucpaasRespCode(String result) {
        JSONObject jsonObject = JSONObject.fromObject(result);
        String respCode = JSONObject.fromObject(jsonObject.getString("resp")).getString("respCode");
        return respCode;
    }

    public static String fileExtensionName(String fileName) {
        int startIndex = fileName.lastIndexOf(".");
        return fileName.substring(startIndex);
    }

    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String extractNumber(String str) {
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    /**
     * 去除字符串中的html标签
     *
     * @param htmlStr 待操作的字符串
     * @return 返回操作后的字符串
     */
    public static String delHTMLTag(String htmlStr) {
        Pattern pW = Pattern.compile(FinalVar.REGEX_W, Pattern.CASE_INSENSITIVE);
        Matcher mW = pW.matcher(htmlStr);
        // 过滤script标签
        htmlStr = mW.replaceAll("");

        Pattern pScript = Pattern.compile(FinalVar.REGEX_SCRIPT, Pattern.CASE_INSENSITIVE);
        Matcher mScript = pScript.matcher(htmlStr);
        // 过滤script标签
        htmlStr = mScript.replaceAll("");

        Pattern pStyle = Pattern.compile(FinalVar.REGEX_STYLE, Pattern.CASE_INSENSITIVE);
        Matcher mStyle = pStyle.matcher(htmlStr);
        // 过滤style标签
        htmlStr = mStyle.replaceAll("");

        Pattern pHtml = Pattern.compile(FinalVar.REGEX_HTML, Pattern.CASE_INSENSITIVE);
        Matcher mHtml = pHtml.matcher(htmlStr);
        // 过滤html标签
        htmlStr = mHtml.replaceAll("");

        Pattern pSpace = Pattern.compile(FinalVar.REGEX_SPACE, Pattern.CASE_INSENSITIVE);
        Matcher mSpace = pSpace.matcher(htmlStr);
        // 过滤空格回车标签
        htmlStr = mSpace.replaceAll("");

        // 过滤
        htmlStr = htmlStr.replaceAll(" ", "");

        // 返回文本字符串
        return htmlStr.trim();
    }

    class FinalVar {
        /**
         * 定义script的正则表达式
         */
        static final String REGEX_SCRIPT = "<script[^>]*?>[\\s\\S]*?<\\/script>";
        /**
         * 定义style的正则表达式
         */
        static final String REGEX_STYLE = "<style[^>]*?>[\\s\\S]*?<\\/style>";
        /**
         * 定义HTML标签的正则表达式
         */
        static final String REGEX_HTML = "<[^>]+>";
        /**
         * 定义空格回车换行符
         */
        static final String REGEX_SPACE = "\\s*|\t|\r|\n";
        /**
         * 定义所有w标签
         */
        static final String REGEX_W = "<w[^>]*?>[\\s\\S]*?<\\/w[^>]*?>";
    }

    public static void main(String[] args) {
        System.out.println(extractNumber("#!=432"));
    }
}

